/*
 * 文件名：CouponDetailServiceImpl.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：hry
 * 修改时间：2016年4月26日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.mall.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.mall.CouponDetail;
import com.zcsy.manage.model.mall.vo.CouponInfoDetailVo;
import com.zcsy.manage.model.mall.vo.CouponInfoVo;
import com.zcsy.manage.model.mall.vo.ShoppingCartVo;
import com.zcsy.manage.persistence.mall.CouponDetailDao;
import com.zcsy.manage.service.mall.ICouponDetailService;

/**
 * 
 * @Title: CouponDetailServiceImpl.java
 * @Package： com.zcsy.manage.service.mall.impl 
 * @Description: 优惠券详情信息service层接口实现类
 *
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年4月26日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class CouponDetailServiceImpl implements ICouponDetailService{
	
	@Autowired
	private CouponDetailDao couponDetailDao;

	@Override
	public int insert(CouponDetail couponDetail) {
		return couponDetailDao.insert(couponDetail);
	}

	@Override
	public int update(CouponDetail couponDetail) {
		return couponDetailDao.update(couponDetail);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		return couponDetailDao.deleteByPrimaryKey(id);
	}

	@Override
	public CouponDetail selectByPrimaryKey(String id) {
		return couponDetailDao.selectByPrimaryKey(id);
	}

	@Override
	public List<CouponDetail> selectByKeyword(CouponDetail couponDetail) {
		return couponDetailDao.selectByKeyword(couponDetail);
	}

	@Override
	public CouponDetail selectByCouponId(String couponid) {
		return couponDetailDao.selectByCouponId(couponid);
	}

	@Override
	public List<CouponDetail> selectDetailByKeyword(CouponDetail couponDetail) {
		// TODO Auto-generated method stub
		return couponDetailDao.selectDetailByKeyword(couponDetail);
	}
	
	@Override
    public List<CouponDetail> selectByUserAccountAndState(CouponDetail couponDetail) {
        return couponDetailDao.selectByUserAccountAndState(couponDetail);
    }
	
	@Override
	public List<CouponDetail> getCouponUsedList(Map<String, Object> map) {
		return couponDetailDao.getCouponUsedList(map);
	}

	@Override
	public List<CouponDetail> selectCouponDetailByKeyword(CouponDetail couponDetail) {
	    return couponDetailDao.selectCouponDetailByKeyword(couponDetail);
	}

	@Override
	public CouponDetail getCountNumerBycouponId(String couponId) {
	    return couponDetailDao.getCountNumerBycouponId(couponId);
	}
	
	@Override
    public CouponDetail getCouponDetailCount(String userAccount) {
	    return couponDetailDao.getCouponDetailCount(userAccount);
	}
	
	@Override
	public CouponDetail selectByUserAccountAndCouponId(String couponId, String loginname) {
	    Map<String,String> map = new HashMap<String, String>();
	    map.put("couponId", couponId);
	    map.put("userAccount", loginname);
	    return couponDetailDao.selectByUserAccountAndCouponId(map);
	}
	
    @Override
    public List<CouponInfoVo> getAvaliabelCoupon(BigDecimal total, List<ShoppingCartVo> cartList, String shopIds, String userAccount) {
        // 店铺id 对应商品总金额
        Map<String, BigDecimal> amountMap = new HashMap<>();
        for(ShoppingCartVo vo : cartList) {
            amountMap.put(vo.getShopId(), vo.getSubTotal());
        }
        
        List<CouponInfoVo> list = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        
        param.put("userAccount", userAccount);
        param.put("shopIds", shopIds.split(","));
        
        
        // 当前用户商店可以人优惠券列表
        List<CouponInfoDetailVo> detailList = couponDetailDao.getAvaliabelCoupon(param);
        
        
        // 按优惠券类别进行分组
        Map<String, List<CouponInfoDetailVo>> map = new HashMap<>();
        for(CouponInfoDetailVo vo : detailList) {
            String key = "";
            
            /**
             * 按照优惠券条件的信息进行筛选
             * 1、平台券：订单总额 >= 减免线
             * 2、商铺券：商铺订单总额 >= 减免线
             */
            boolean flag = false;
            if(Constants.COUPON_TYPE_FORUM == vo.getType()) {
                if(total.compareTo(vo.getBreakLine()) >= 0) {
                    key = vo.getType().toString();
                    flag = true;
                }
            } else {
                if(amountMap.get(vo.getShopId()).compareTo(vo.getBreakLine()) >= 0) {
                    key = vo.getShopId();
                    flag = true;
                }
            }
            if(flag) {
                if(map.containsKey(key)) {
                    map.get(key).add(vo);
                } else {
                    List<CouponInfoDetailVo> temp = new ArrayList<>();
                    temp.add(vo);
                    map.put(key, temp);
                }
            }
        }
        CouponInfoVo info = null;
        for(String key : map.keySet()) {
            info = new CouponInfoVo();
            info.setCouponType(map.get(key).get(0).getType());
            info.setShopName(map.get(key).get(0).getShopName());
            info.setShopId(map.get(key).get(0).getShopId());
            info.setDetailList(map.get(key));
            
            list.add(info);
        }
        return list;
    }
}
